package Window;
/*
 * Az osztály a ütemező panelt valósítja meg. A panelen beállítható, 
 * hogy hány ütemezési ciklust futtasson le a program, az ütemezés a Schedule gombbal indítható.
 * */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import ModelAndController.Controller;

public class StepPanel {

	//view peldany
	protected View view;
	//controller peldany
	protected Controller controller;
	protected boolean isCircuitLoaded;
	
	protected int panelWidth;
	protected int panelHeight;
	protected JPanel stepPanel;
	protected Box horizontalBox;
	
	//utemezes szamat tartalmazza
	protected JTextField nbOfStepsTextField;
	
	//Kontroller
	public StepPanel(View view, Dimension windowSize, Controller controller, boolean isCircuitLoaded){
		this.view = view;
		this.controller = controller;
		this.isCircuitLoaded = isCircuitLoaded;
		
		panelWidth = windowSize.width;
		panelHeight = (int)(windowSize.height*0.1);
		initPanelAndBox();
		initBoxElements();
		createBorderForPanel();
	}
	
	//Visszaadja a panelt
	public JPanel getPanel(){
		return stepPanel;
	}
	
	//A horizontalBox beallitasa
	private void initPanelAndBox(){
		BorderLayout border = new BorderLayout();
		stepPanel = new JPanel(border);
		
		Dimension panelSize = new Dimension(panelWidth, panelHeight);
		stepPanel.setMaximumSize(panelSize);
		stepPanel.setPreferredSize(panelSize);
		
		horizontalBox = Box.createHorizontalBox();
	}
	
	//horizontalBox elemeinek letrehozasa
	private void initBoxElements(){
		JLabel stepNumberLabel = new JLabel("Number of schedule: ");
		horizontalBox.add(stepNumberLabel);
		
		nbOfStepsTextField = new JTextField("1");
		Dimension nbOfStepsTextFieldSize = new Dimension((int)(panelWidth*0.04), (int)(panelHeight*0.5));
		nbOfStepsTextField.setMinimumSize(nbOfStepsTextFieldSize);
		nbOfStepsTextField.setMaximumSize(nbOfStepsTextFieldSize);
		nbOfStepsTextField.setPreferredSize(nbOfStepsTextFieldSize);
		horizontalBox.add(nbOfStepsTextField);
		
		JButton stepButton = new JButton("Schedule");
		stepButton.setEnabled(isCircuitLoaded);
		ALForSaveButton alForSaveButton = new ALForSaveButton();
		stepButton.addActionListener(alForSaveButton);
		horizontalBox.add(stepButton);
		
		horizontalBox.add(Box.createHorizontalGlue());
	}
	
	//Border letrehozas
	private void createBorderForPanel(){
		stepPanel.add(horizontalBox);
		stepPanel.setBorder(BorderFactory.createTitledBorder("Scedule"));
	}
	
	//Scedule gomb actionListenerje
	private class ALForSaveButton implements ActionListener{
		public void actionPerformed(ActionEvent e){
			InputPanel inputPanel = view.getInputPanel();
			inputPanel.updateSwitchValues();
			
			int nbOfSteps = -1;
			try{
				nbOfSteps = Integer.parseInt(nbOfStepsTextField.getText());
			}catch(Exception exp){
				nbOfStepsTextField.setText("1");
				view.catchException(exp);
			}
			if( nbOfSteps < 1){
				nbOfStepsTextField.setText("1");
				view.catchException( new Exception("Number of steps should be greated then 0, but got: "+nbOfSteps) );
				return;
			}
			controller.step(nbOfSteps);
		}
	}
}
